class String
  def clean_colors; gsub(/\x1B\[.+?m/, '') end
  def clean_colors!; gsub!(/\x1B\[.+?m/, '') end
end

class RubyMudColors
  COLOR_ATTR = {'30' => {'set' => 'foreground', 'base' => '#000000', 'bold' => '#646464'},
                '31' => {'set' => 'foreground', 'base' => '#B40000', 'bold' => '#FF0000'},
                '32' => {'set' => 'foreground', 'base' => '#00B400', 'bold' => '#00FF00'},
                '33' => {'set' => 'foreground', 'base' => '#B4B400', 'bold' => '#FFFF00'},
                '34' => {'set' => 'foreground', 'base' => '#0000B4', 'bold' => '#0000FF'},
                '35' => {'set' => 'foreground', 'base' => '#B400B4', 'bold' => '#FF00FF'},
                '36' => {'set' => 'foreground', 'base' => '#00B4B4', 'bold' => '#00FFFF'},
                '37' => {'set' => 'foreground', 'base' => '#B4B4B4', 'bold' => '#FFFFFF'},
                '39' => {'set' => 'foreground', 'base' => '#B4B4B4', 'bold' => '#FFFFFF'},
                '40' => {'set' => 'background', 'base' => '#000000'},
                '41' => {'set' => 'background', 'base' => '#B40000'},
                '42' => {'set' => 'background', 'base' => '#00B400'},
                '43' => {'set' => 'background', 'base' => '#B4B400'},
                '44' => {'set' => 'background', 'base' => '#0000B4'},
                '45' => {'set' => 'background', 'base' => '#B400B4'},
                '46' => {'set' => 'background', 'base' => '#00B4B4'},
                '47' => {'set' => 'background', 'base' => '#B4B4B4'},
                '49' => {'set' => 'background', 'base' => '#B4B4B4'}}
  @@override ||= Hash.new
  
  def self.add_override(key, tag)
    @@override[key] = tag
  end
  
  def initialize(control)
    @control = control
    COLOR_ATTR.each { |k,v|
      @control.tag_configure("#{k}base", v['set'] => v['base'])
      @control.tag_configure("#{k}bold", v['set'] => v['bold']) if v.key?('bold')
    }
    @control.tag_configure('u', :underline => true)
    self.set_tags
  end
  
  def set_tags(style = '0')
    if style == '0'
      @current = {'foreground' => '37', 'background' => '40', 'bold' => false, 'u' => '', 'i' => ''}
    elsif style == '1'
      @current['bold'] = true
    elsif style == '3'
      @current['i'] = ' i'
    elsif style == '4' || style == '5'
      @current['u'] = ' u'
    elsif style == '7'
      @current['foreground'] = "#{@current['background'].to_i-10}"
      @current['background'] = "#{@current['foreground'].to_i+10}"
    elsif @@override.key?(style)
      @current['override'] = @@override[style]
    elsif COLOR_ATTR.key?(style)
      @current['bold'] = false
      @current[COLOR_ATTR[style]['set']] = style
    else
      puts("- RubyMud: Got Ansi value |#{style}|")
    end
  end
  
  def get_tags
    return @current['override'] if @current.key?('override')
    fg = "#{@current['foreground']}#{@current['bold'] ? 'bold' : 'base'}"
    bg = "#{@current['background']}base"
    u = @current['u'] ? ' u' : ''
    i = @current['i'] ? ' i' : ''
    "#{fg} #{bg}#{@current['u']}#{@current['i']}"
  end
end